import urllib.request
import random


def load_baidu():


    #模拟真实的浏览器发送请求
    #应用场景：批量搜索
    user_agent_list = [
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1",
        "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
        "Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50",
        "Mozilla/5.0 (Windows; U; Windows NT 6.1; ) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12"
    ]

    url = "http://www.baidu.com/"

    # 每次请求的浏览器都是不一样的
    header = {
        'User-Agent': random.choice(user_agent_list)
    }

    # 创建请求对象
    request = urllib.request.Request(url, headers=header)

    # 请求网络数据
    response = urllib.request.urlopen(request)
    # print(response.headers)  #响应头
    data = response.read().decode('utf-8')
    # print(data)


    # 获取请求头的信息
    print(request.headers)
    # print(request.get_header('User-agent'))


load_baidu()
